from instance import instance
from graph import graph
import networkx as nx

class noclock:
  def __init__(self,gr,ins):
    self.g = graph(gr)
    self.g.toNum()
    self.ins = instance(ins)
    self.clkD = ["FDSE","FDCE","FDRE"]
    self.clkRAM = ["RAMB36E1","RAMB18E1"]
    self.clk = self.clkD + self.clkRAM
    self.clkinsList = []
    for k in self.clk:
      for i in self.ins.getSome(k).keys():
        if self.g.lookup.__contains__(i):
          self.clkinsList.append(self.g.lookup[i]) 
    self.noc = graph("")
    self.clkins = set(self.clkinsList)
    for u,v,d in self.g.nG.edges(data='weight'):
      if u not in self.clkins and v not in self.clkins:
        self.noc.G.add_edge(self.g.name[u], self.g.name[v], weight=d)
        self.noc.nG.add_edge(u, v, weight=d)
  def toNode(self):
    self.noc.toNode()

  def subG(self):
    self.subnoc = []
    nG = self.noc.nG.to_undirected()
    for node in nx.connected_components(nG):
      self.subnoc.append(node)

  def group(self,g):
    nodes = self.subnoc[g]
    return self.noc.nG.subgraph(nodes)

  def toGNode(self,g):
    for u,v,d in g.edges(data='weight'):
      print(u,v,d)


def main():
  noc = noclock("../test_flatten.g","../test_flatten.ins")
  # noc.toNode()
  noc.subG()
  # for nodes in noc.subnoc:
  #   print(len(nodes))
  # for n in noc.subnoc[0]:
  #   name = noc.g.name[n]
  #   print(name,noc.ins.lookup[name])
  noc.toGNode(noc.group(0))

if __name__ == '__main__':
  main()